home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Magnum One
/
Magnum One (Mid-American Digital) (Disc Manufacturing).iso
/
d12
/
cgazv5n5.arc
/
EVENT.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-09-23
|
17KB
|
533 lines
/***************************************************************
* File: Event.c -
* Purpose: Starts routines or macros at an arbitrary time
* Author: Kanhom Ng
* P.O. Box 391176, Mountain View, CA 94039
*
* Compiler: Microsoft C 6.00a
*
* Source code may be used freely if source is acknowledged.
* Object code may be used freely.
**************************************************************/
#define NOCOMM
#include <windows.h>
#include <stdio.h>
#include <memory.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#include "event.h"
#define ID_LISTBOX 1000 /* id of the list box */
#define MAX_EVENT 20 /* maximum event allowed */
#define Date2Str(t,s) \
wsprintf( s, "%d/%d/%d", (t).tm_mon + 1, \
(t).tm_mday, (t).tm_year )
#define Time2Str(t,s) \
wsprintf( s, "%d:%.2d", (t).tm_hour, (t).tm_min )
#define Str2Date(s,t) \
( 3 == sscanf( s, "%d/%d/%d", &((t).tm_mon), \
&((t).tm_mday), &((t).tm_year) ) && (t).tm_mon-- )
#define Str2Time(s,t) \
( 2 == sscanf( s, "%d:%d", &((t).tm_hour), \
&((t).tm_min) ) )
/* global variables */
static EVENTREC aEventList[ MAX_EVENT ]; /* event list */
static int anDateFirstTabStop[2]
= { 48, 100 }; /* list box's tab stops */
static PEVENTREC pEventRec; /* dummy dialog box parameter */
/* function prototypes */
LONG FAR PASCAL EventCenterWndProc( HWND, WORD, WORD, LONG );
BOOL FAR PASCAL _export EventDlgProc( HWND, WORD, WORD, LONG );
HANDLE PASCAL SpawnApp( LPSTR lpszCmdLine );
/*
* WinMain( hInstance, hPrevInst, lpszCmd ) : int;
*
* This is the entry point for the Event application
*
*/
int PASCAL WinMain(
HANDLE hInstance,
HANDLE hPrevInst,
LPSTR lpszCmd,
WORD wCmdShow )
{
MSG msg; /* MSG Structure for message loop */
HWND hwndEvent; /* handle to the windows */
WNDCLASS WndClass; /* WNDCLASS structure */
if ( hPrevInst == 0 ) {
_fmemset( &WndClass, 0, sizeof( WndClass ) );
WndClass.lpfnWndProc = EventCenterWndProc;
WndClass.hInstance = hInstance;
WndClass.hIcon = LoadIcon( NULL, IDI_APPLICATION );
WndClass.hCursor = LoadCursor( NULL, IDC_ARROW );
WndClass.hbrBackground = ( COLOR_APPWORKSPACE + 1 );
WndClass.lpszMenuName = "CenterMain";
WndClass.lpszClassName = "EventCenter";
RegisterClass( &WndClass );
}
hwndEvent = CreateWindow(
"EventCenter",
"Event Scheduler",
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL );
ShowWindow( hwndEvent, wCmdShow );
/* main message loop */
while ( GetMessage( &msg, NULL, 0, 0 ) ) {
TranslateMessage( &msg );
DispatchMessage( &msg );
}
return ( NULL );
}
/*
* EventCenterWndProc( hwndEvent, wMessage, wParam, lParam ) : LONG;
*
*
* This is the window function for Event's Main window.
*/
LONG FAR PASCAL EventCenterWndProc(
HWND hwndEvent,
WORD wMessage,
WORD wParam,
LONG lParam
)
{
BOOL fSuccess; /* were we successful? */
LONG lReturn; /* return value of this function */
HANDLE hInstance; /* handle to the instance */
HWND hwndRecorder;/* handle to Recorder's main window */
RECT rc; /* current window client rect */
int i, j; /* loop variables */
int iSel; /* index to the current selection */
int cEvent; /* count of the event */
int cString; /* string # in Recorder's listbox */
char szText[240]; /* temporary text string */
char szTime[20]; /* temporary string for time */
char szDate[20]; /* temporary string for date */
FARPROC lpfnEvent; /* event dialog box function */
struct tm tmCurTime;/* current time */
time_t lCurTime; /* current time */
time_t lEventTime; /* time specified in the event record */
/* initialization */
lReturn = 0L;
fSuccess = FALSE;
switch ( wMessage ) {
case WM_CREATE:
/* create a listbox window to display all events */
CreateWindow(
"listbox", NULL,
LBS_NOTIFY | LBS_NOINTEGRALHEIGHT | WS_CHILD |
WS_VISIBLE | LBS_USETABSTOPS,
0, 0, 0, 0,
hwndEvent, ID_LISTBOX,
GetWindowWord( hwndEvent, GWW_HINSTANCE ), NULL );
/* initialize the tab stop for the listbox */
SendDlgItemMessage( hwndEvent, ID_LISTBOX,
LB_SETTABSTOPS, 2, (LONG)(LPINT)anDateFirstTabStop);
/* initialize the timer */
SetTimer( hwndEvent, 1, 5000, NULL );
/* initialize the array of events */
_fmemset( aEventList, 0, sizeof( aEventList ) );
/* send self a WM_TIMER message to update title */
PostMessage(hwndEvent, WM_TIMER, 0, 0L);
break;
case WM_DESTROY:
KillTimer( hwndEvent, 1 );
PostQuitMessage(0);
break;
case WM_TIMER:
/* retrieve count of # of pending events */
cEvent = (int) SendDlgItemMessage( hwndEvent,
ID_LISTBOX, LB_GETCOUNT, 0, 0l );
/* retrieve the current time adjusted for local time */
lCurTime = time( NULL );
tmCurTime = *localtime( &lCurTime );
lCurTime = mktime( &tmCurTime );
/* update window's title bar with current time */
wsprintf(szText, "Event Scheduler: %02d:%02d:%02d",
tmCurTime.tm_hour,
tmCurTime.tm_min,
tmCurTime.tm_sec);
SetWindowText(hwndEvent, szText);
for ( i = 0; i < cEvent; i ++ )
{
/* Should this event be executed at this time? */
if (FALSE == aEventList[ i ].fCalled
&& (lEventTime = mktime( &aEventList[ i ].tm))
&& (lEventTime != -1 && lEventTime <= lCurTime))
{
aEventList[i].fCalled = TRUE;
SpawnApp( aEventList[ i ].szCmd );
/* need to replay a macro ? */
if ( aEventList[ i ].fRecorder )
{
/* spawn Recorder with the specified file */
SpawnApp( aEventList[ i ].szFile );
hwndRecorder = FindWindow("recorder", NULL);
if ( hwndRecorder )
{
/* retrieve count of string in
Recorder's list box */
cString = (int)SendDlgItemMessage(
hwndRecorder, 60, LB_GETCOUNT, 0, 0l);
/* find our macro in the listbox */
for ( j = 0; j < cString; j++ )
{
/* retrieve the string text from
* the listbox and compare it with
* the user's macro name. */
fSuccess = SendDlgItemMessage(
hwndRecorder, 60, LB_GETTEXT,
j, (LONG)(LPSTR)szText ) >= 0;
/* is it our macro? when checking
* the name, must skip over the TAB
* that Recorder places before the
* macro's name. Thus, we compare to
* szText[1], not szText[0]. */
if ( fSuccess && lstrcmpi(
&szText[1],
aEventList[ i ].szMacro ) == 0 )
{
/* select this string in the
* listbox and send a message
* that runs the macro. */
SendDlgItemMessage(hwndRecorder,
60, LB_SETCURSEL, j, 0L );
SendMessage( hwndRecorder,
WM_COMMAND, 0x0011, 0L );
/* shut down recorder */
SendMessage( hwndRecorder,
WM_COMMAND, 0x000E, 0L );
break;
}
}
}
}
}
}
break;
case WM_SIZE: /* make sure our list box fits */
GetClientRect( hwndEvent, &rc );
MoveWindow( GetDlgItem( hwndEvent, ID_LISTBOX ),
rc.left, rc.top, rc.right - rc.left,
rc.bottom - rc.top, TRUE );
break;
case WM_SETFOCUS: /* let the listbox have the focus */
SetFocus( GetDlgItem( hwndEvent, ID_LISTBOX ) );
break;
case WM_INITMENU: /* gray the Delete if no event is
currently selected */
iSel = (int)SendDlgItemMessage( hwndEvent, ID_LISTBOX,
LB_GETCURSEL, 0, 0l );
EnableMenuItem( GetMenu( hwndEvent ), IDM_DELETE,
iSel >= 0 ? MF_ENABLED : MF_GRAYED );
break;
case WM_COMMAND:
switch ( wParam )
{
case IDM_NEW:
cEvent = (int)SendDlgItemMessage( hwndEvent,
ID_LISTBOX, LB_GETCOUNT, 0, 0l );
if ( cEvent < MAX_EVENT )
{
/* initialization */
_fmemset( &aEventList[ cEvent ], 0,
sizeof( aEventList[ cEvent ] ) );
hInstance = GetWindowWord( hwndEvent,
GWW_HINSTANCE );
lpfnEvent = MakeProcInstance(
(FARPROC)EventDlgProc, hInstance );
pEventRec = &aEventList[ cEvent ];
if (-1 != DialogBox( hInstance, "Event",
hwndEvent, lpfnEvent )
&& Time2Str( aEventList[ cEvent ].tm,
szTime )
&& Date2Str( aEventList[ cEvent ].tm,
szDate ) )
{
/* format the event into a string
* and add to our listbox */
wsprintf(szText, "%s\t%s\t%s",
(LPSTR)szDate, (LPSTR)szTime,
(LPSTR)aEventList[ cEvent ].szDesc );
SendDlgItemMessage( hwndEvent,
ID_LISTBOX, LB_ADDSTRING, 0,
(LONG)(LPSTR)szText );
}
FreeProcInstance( lpfnEvent );
}
break;
case IDM_DELETE:
iSel = (int)SendDlgItemMessage( hwndEvent,
ID_LISTBOX, LB_GETCURSEL, 0, 0l );
cEvent = (int)SendDlgItemMessage( hwndEvent,
ID_LISTBOX, LB_GETCOUNT, 0, 0l );
SendDlgItemMessage( hwndEvent, ID_LISTBOX,
LB_DELETESTRING, iSel, 0l );
/* move the remaining slot 1 up */
_fmemcpy( &aEventList[ iSel ],
&aEventList[ iSel + 1 ],
sizeof(aEventList[iSel]) * (cEvent - iSel - 1));
break;
case IDM_EXIT:
DestroyWindow( hwndEvent );
break;
}
break;
default:
lReturn = DefWindowProc( hwndEvent, wMessage,
wParam, lParam );
break;
}
return ( lReturn );
}
/*
* EventDlgProc( hwndDlg, wMessage, wParam, lParam ) : BOOL;
*
* This is the function for the NEW dialog box.
*
*/
BOOL FAR PASCAL _export EventDlgProc(
HWND hwndDlg,
WORD wMessage,
WORD wParam,
LONG lParam
)
{
BOOL fReturn; /* return value of this function */
char szTime[40]; /* temporary string for the time */
char szDate[40]; /* temporary string for the date */
time_t lTime; /* current time value */
struct tm tm; /* structure for the time functions */
/* initialization */
fReturn = TRUE;
switch ( wMessage )
{
case WM_INITDIALOG:
/* initilize the Date and Time
field to be current date and time */
lTime = time( NULL );
tm = *localtime( &lTime );
Time2Str( tm, szTime );
Date2Str( tm, szDate );
SetDlgItemText( hwndDlg, ID_TIME, szTime );
SetDlgItemText( hwndDlg, ID_DATE, szDate );
break;
case WM_COMMAND:
switch ( wParam )
{
case IDOK:
/* 1. use the time()/localtime() to fill
* out tm_isdst (daylight saving time)
* 2. retrieve the date and time from the user
*/
lTime = time( NULL );
tm = *localtime( &lTime );
tm.tm_sec = 0;
GetDlgItemText( hwndDlg, ID_DATE, szDate,
sizeof( szDate ) );
GetDlgItemText( hwndDlg, ID_TIME, szTime,
sizeof( szTime ) );
/* did the user specify a valid date and time ? */
if ( Str2Date( szDate, tm ) && Str2Time(szTime, tm))
{
/* retrieve the description and command line */
GetDlgItemText( hwndDlg, ID_DESC,
pEventRec->szDesc,
sizeof( pEventRec->szDesc ) );
GetDlgItemText( hwndDlg, ID_CMDLINE,
pEventRec->szCmd,
sizeof( pEventRec->szCmd ) );
/* retrieve whether to replay a macro,
and the file and the macro name */
pEventRec->fRecorder = IsDlgButtonChecked(
hwndDlg, ID_RUNRECORDER );
GetDlgItemText( hwndDlg, ID_FILE,
pEventRec->szFile,
sizeof( pEventRec->szFile ) );
GetDlgItemText( hwndDlg, ID_MACRONAME,
pEventRec->szMacro,
sizeof( pEventRec->szMacro ) );
pEventRec->tm = tm;
EndDialog( hwndDlg, TRUE );
}
else {
SHOWERR( FALSE, hwndDlg,
"Incorrect date or time specified");
}
break;
case IDCANCEL:
EndDialog( hwndDlg, -1 );
break;
}
break;
default:
fReturn = FALSE;
break;
}
return ( fReturn );
}
/*
* SpawnApp( lpszCmdLine ) : VOID;
*
* This function will spawn an application. It looks up the
* win.ini extension section to insert the proper parameter
* before calling WinExec(). as WinExec() will only take care
* of the standard extensions .exe and .pif.
*
*/
HANDLE PASCAL SpawnApp(
LPSTR lpszCmdLine
)
{
BOOL fSuccess; /* did we succeed? */
char szAppName[ 160]; /* build command line here */
char *p; /* temporary pointer */
char *pDot; /* temporary pointer to '.' + 1 */
HANDLE hModule; /* handle to the loaded module */
char szExt[4]; /* extension string */
HCURSOR hOldCursor; /* handle to the old cursor */
/* Change to waiting cursor */
hOldCursor = SetCursor( LoadCursor( NULL, IDC_WAIT ) );
/* copy the app name and parameter */
lstrcpy( szAppName, lpszCmdLine );
szAppName[ sizeof( szAppName ) -1 ] = 0;
/* find out where the parameters start */
p = strchr( szAppName, ' ' );
/* Are there any parameters at all? */
if ( NULL == p )
{
/* no parameters, therefore it could be an extension */
/* find the extension */
pDot = strchr( szAppName, '.' );
if ( pDot )
{
/* Is this a standard extension? */
if (_fstrnicmp( pDot + 1, "exe", 3 ) == 0
|| _fstrnicmp( pDot + 1, "pif", 3 ) == 0 )
{
/* standard WINDOWS extension */
/* which WinExec() will handle */
}
else { /* nonstandard extension */
/* extract the extension */
_fstrncpy( szExt, pDot + 1, 3 );
szExt[ 3 ] = 0;
/* try to find the [Extension] in win.ini
* to figure out which application will
* handle this extension */
fSuccess = GetProfileString(
"Extensions", szExt, "",
szAppName, sizeof( szAppName ) ) > 0
&& lstrcmp( szAppName, "" ) != 0;
/* Correct extension specified in win.ini ? */
if ( fSuccess )
{
/* now the szAppName should look like this:
* "notepad.exe ^.txt"
*/
/* find out where the parameters start */
p = strchr( szAppName, ' ' );
/* expected format ? */
if ( p )
{
/* cut off the parameters */
*( p + 1 ) = 0;
/* now szAppName looks like this:
* "notepad.exe "
*/
}
else
{
/* UNexpected format */
/* clean all the junk */
szAppName[ 0 ] = 0;
}
}
/* append the Original Spawn statement to the
* end of the proper application name. */
_fstrncat( szAppName, lpszCmdLine,
sizeof( szAppName ) );
/* ensure it is NULL-terminated */
szAppName[ sizeof( szAppName ) - 1 ] = 0;
}
}
}
hModule = WinExec( szAppName, SW_SHOW );
hModule = ( hModule < 32 ) ? hModule : 0;
/* Change back to original */
if ( hOldCursor )
{
SetCursor( hOldCursor );
}
return ( hModule );
}